<?php
class model_interview extends model_weixin{
	
	/**
	 * @info 新增走訪信息
	 * @author jackie yang
	 * @datetime 20140705
	 * @param  $data  array
	 * @param $table string 数据表
	 */
	public static function add_interview_record($data,$table) {
		$sql = "INSERT INTO " . $table . " SET ";
		$binds = array ();
		foreach ( $data as $k => $v ) {
			$sql .= '`' . $k . '`=:' . $k . ',';
			$binds [':' . $k] = $v;
		}
		$sql .= 'raw_add_time=NOW(),raw_update_time=now()';
		try {
			$arr = self::DB ()->dbh->prepare ( $sql );
			$rel = $arr->execute ( $binds );
			if ($rel == TRUE) {
				return self::DB ()->dbh->lastInsertId ();
			}
		} catch ( Exception $e ) {
			framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
		}
		return 0;
	}
	/**
	 * 
	 * 获取售前走访记录
	 * @param string $openid
	 * @param string $visit_unit 公司单位，走访单位
	 */
	public static function get_interview_record_from_weixin($openid,$visit_unit,$pageSize=5) {
		$data = array();
		$sql = "select * from weixin_visit_log where visit_unit=:visit_unit and delete_flag=0 and sales_id like '%".$openid."%' order by visit_log_id desc limit ".$pageSize;
		try {
    		$binds = array(
    			':visit_unit' => $visit_unit
    		);
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		$data = $st->fetchAll();
    	}catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
	}
	/**
	 * 
	 * 根据公司名称获取售后答复内容
	 * @param string $customer_name
	 * @return array $data
	 */
	public static function get_aftermarket_record($customer_name) {
		$data = array();
		/*$sql = "SELECT qs.customer_info_id,ci.customer_name,repeat_content
				FROM 
					weixin_question_solution qs,weixin_customer_info ci 
				WHERE 
					qs.customer_info_id = ci.customer_info_id AND ci.customer_name=:customer_name AND ci.delete_flag=0 
				order by 
					question_solution_id desc limit 1";*/

        $sql = "SELECT qs.customer_info_id,ci.customer_name,repeat_content
                FROM weixin_customer_info ci left join weixin_question_solution qs on qs.customer_info_id = ci.customer_info_id
                where  ci.customer_name=:customer_name AND ci.delete_flag=0
                order by question_solution_id desc limit 1 ";

		try {
    		$binds = array(
    			':customer_name' => $customer_name
    		);
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds);
    		$data = $st->fetch();
    	}catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
	}


    /*
     * 获取归属单位
     */
    public static function getUnitCategory(){
        $data = array();
        $sql = "select * from weixin_unit_category order by unit_category_id asc ";
        try {
            $binds = array();
            $st = self::DB()->dbh->prepare($sql);
            $st->execute($binds);
            $data = $st->fetchAll();
        }catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
    }

    /**
     * 插入数据
     */
    public static function insertIntoVisited($data){
        $sql = "INSERT INTO weixin_customer_visited SET ";
        $binds = array ();
        foreach ( $data as $k => $v ) {
            $sql .= '`' . $k . '`=:' . $k . ',';
            $binds [':' . $k] = $v;
        }
        $sql .= ' raw_add_time=NOW(),raw_update_time=now()';
        try {
            $arr = self::DB ()->dbh->prepare ( $sql );
            $rel = $arr->execute ( $binds );
            if ($rel == TRUE) {
                return self::DB ()->dbh->lastInsertId ();
            }
        } catch ( Exception $e ) {
            framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
        }
        return 0;
    }

    /**
     * 查询
     * @param $key
     * @param $pageSize
     * @return array
     */

    public static function searchVisit($key,$visible,$pageSize=30){
        $data = array();
        $sql = "select * from weixin_customer_visited where visit_custorm like '%".$key."%' and delete_flag=0 and status = 1 and ( visible='*_*' or visible like '%".$visible."%') order by visiter_date desc limit ".$pageSize;
        try {
            $binds = array();
            $st = self::DB()->dbh->prepare($sql);
            $st->execute($binds);
            $data = $st->fetchAll();
        }catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
    }

    public static function getVisitInfo($id,$openId){
        $data = array();
        $sql = "select * from weixin_customer_visited where customer_visited_id =:id and delete_flag=0 and status = 1 ";
        try {
            $binds = array(
                ':id' => $id
            );
            $st = self::DB()->dbh->prepare($sql);
            $st->execute($binds);
            $data = $st->fetch();
        }catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
    }


    /**
     * 插入数据
     */
    public static function insertIntoOrder($data){
        $sql = "INSERT INTO weixin_purch_plan SET ";
        $binds = array ();
        foreach ( $data as $k => $v ) {
            $sql .= '`' . $k . '`=:' . $k . ',';
            $binds [':' . $k] = $v;
        }
        $sql .= 'raw_add_time=NOW(),raw_update_time=now()';
        try {
            $arr = self::DB ()->dbh->prepare ( $sql );
            $rel = $arr->execute ( $binds );
            if ($rel == TRUE) {
                return self::DB ()->dbh->lastInsertId ();
            }
        } catch ( Exception $e ) {
            framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
        }
        return 0;
    }

    public static function getUserTypeList($type = 1){
        $data = array();
        $sql = "select user_category_id from weixin_user_category where parent_id = :type ";
        try {
            $binds = array(':type'=>$type);
            $st = self::DB()->dbh->prepare($sql);
            $st->execute($binds);
            $data = $st->fetchAll();
        }catch(Exception $e){
            framework_static_function::write_log(__METHOD__.":".$e->getMessage(),'weixin_sql');
        }
        return $data;
    }



}